package org.joy.db2;

import java.io.Closeable;
import java.io.IOException;

import com.sleepycat.db.Cursor;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.LockMode;
import com.sleepycat.db.OperationStatus;

public class DBCursor implements Closeable {
    private Cursor c;
    private DatabaseEntry key = new DatabaseEntry(), val = new DatabaseEntry();

    public DBCursor(Cursor c) {
	this.c = c;
    }

    public boolean next() throws IOException {
	OperationStatus s;
	try {
	    s = c.getNext(key, val, LockMode.DEFAULT);
	    return s == OperationStatus.SUCCESS;
	} catch (DatabaseException e) {
	    throw new IOException(e);
	}
    }

    public DatabaseEntry getKey() {
	return key;
    }

    public DatabaseEntry getVal() {
	return val;
    }

    @Override
    public void close() throws IOException {
	try {
	    c.close();
	} catch (DatabaseException e) {
	    throw new IOException(e);
	}
    }
}
